<?php
/**
 * messageStack Class.
 *
 * @package classes
 * @copyright Copyright 2003-2006 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: message_stack.php 5384 2006-12-24 19:38:39Z drbyte $
 */
if( ! defined( 'IS_ADMIN_FLAG' ) )
{
	die( 'Illegal Access' );
}
/**
 * messageStack Class.
 * This class is used to manage messageStack alerts
 *
 * @package classes
 */
class MessageStack
{

	// class constructor
	function messageStack()
	{
		
		$this->messages = array( );
		
		if( edp_isset_session( 'messageToStack' ) )
		{
			$messageToStack =  edp_getsession( 'messageToStack' );
			for( $i = 0, $n = count( $messageToStack ); $i < $n; $i ++ )
			{
				$this->add( $messageToStack[$i]['class'], $messageToStack[$i]['text'], $messageToStack[$i]['type'] );
			}
			edp_unset_session( 'messageToStack' );
			
		}
	}

	// class methods
	function add( $class, $message, $type = 'error' )
	{
		global $template, $current_page_base;
		
		$message = trim( $message );
		if( strlen( $message ) > 0 )
		{
			if( $type == 'error' )
			{
				$this->messages[] = array( 'params' => 'class="messageStackError"', 'class' => $class, 'text' => '<span>'.$message.'</span>' );
			} elseif( $type == 'warning' )
			{
				$this->messages[] = array( 'params' => 'class="messageStackWarning"', 'class' => $class, 'text' => '<span>'.$message.'</span>' );
			} elseif( $type == 'success' )
			{
				$this->messages[] = array( 'params' => 'class="messageStackSuccess"', 'class' => $class, 'text' => '<span>'.$message.'</span>' );
			} elseif( $type == 'caution' )
			{
				$this->messages[] = array( 'params' => 'class="messageStackCaution"', 'class' => $class, 'text' => '<span>'.$message.'</span>' );
			} else
			{
				$this->messages[] = array( 'params' => 'class="messageStackError"', 'class' => $class, 'text' => '<span>'.$message.'</span>' );
			}
		}
	}

	function add_session( $class, $message, $type = 'error' )
	{
		
		if(  !edp_isset_session( 'messageToStack' ) )
			$messageToStack = array( ); 
		else
			$messageToStack = edp_getsession( 'messageToStack' );
		
		$messageToStack[] = array( 'class' => $class, 'text' => $message, 'type' => $type );
		$this->add( $class, $message, $type );
		
		edp_setsession( 'messageToStack', $messageToStack );
		
	}

	function reset()
	{
		$this->messages = array( );
	}

	function output( $class )
	{
		global $template, $current_page_base;
		
		$output = array( );
		for( $i = 0, $n = sizeof( $this->messages ); $i < $n; $i ++ )
		{
			if( $this->messages[$i]['class'] == $class ||
				$this->messages[$i]['class'] == 'EVF' )
			{
				$output[] = $this->messages[$i];
			}
		}
		
		if( count($output)  == 0)
			return;
		
		echo "<div style='clear:both'></div><div " . $output[0]['params'] . ">" ;
		foreach( $output as $i => $key  )
			echo $key['text'] . "<br/>";  
		echo "</div>";
		
		edp_unset_session( 'messageToStack' );
		
	
	}
	
	function getMessage( $class )
	{
		global $template, $current_page_base;
		
		$output = array( );
		for( $i = 0, $n = sizeof( $this->messages ); $i < $n; $i ++ )
		{
			if( $this->messages[$i]['class'] == $class ||
				$this->messages[$i]['class'] == 'EVF' )
			{
				$output[] = $this->messages[$i];
			}
		}
		
		if( count($output)  == 0)
			return;
		
		$msg = "<div style='clear:both'></div><div " . $output[0]['params'] . ">" ;
		foreach( $output as $i => $key  )
			$msg .= $key['text'] . "<br/>";  
		$msg .=  "</div>";
		
		edp_unset_session( 'messageToStack' );
		return $msg;
		
	
	}
	
	function outputPlain( $class )
	{
		global $template, $current_page_base;
		
		$output = array( );
		for( $i = 0, $n = count( $this->messages ); $i < $n; $i ++ )
		{
			if( $this->messages[$i]['class'] == $class ||
				$this->messages[$i]['class'] == 'EVF' )
			{
				$temp = $this->messages[$i];
				$temp = preg_replace("/<span>/", "", $temp );
				$temp = preg_replace("/<\/span>/", "", $temp );
				$output[] = $temp;
			}
		}
		
		if( count($output)  == 0)
			return;
		
		
		foreach( $output as $i => $key  )
			echo $key['text'] . "<br/>";  
			
		edp_unset_session( 'messageToStack' );
		
	}
	
	function getPlain( $class )
	{
		global $template, $current_page_base;
		
		$output = array( );
		for( $i = 0, $n = count( $this->messages ); $i < $n; $i ++ )
		{
			if( $this->messages[$i]['class'] == $class ||
				$this->messages[$i]['class'] == 'EVF' )
			{
				$temp = $this->messages[$i];
				$temp = preg_replace("/<span>/", "", $temp );
				$temp = preg_replace("/<\/span>/", "", $temp );
				$output[] = $temp;
			}
		}
		
		if( count($output)  == 0)
			return;
		
		$msg = "";
		foreach( $output as $i => $key  )
			$msg .= $key['text'] . "<br/>";  
			
		edp_unset_session( 'messageToStack' );
		
		return $msg;
		
	}

	function size( $class )
	{
		$count = 0;
		
		for( $i = 0, $n = sizeof( $this->messages ); $i < $n; $i ++ )
		{
			if( $this->messages[$i]['class'] == $class ||
				$this->messages[$i]['class'] == 'EVF' )
			{
				$count ++;
			}
		}
		
		return $count;
	}
}
?>